package com.yang.sms;

import com.alibaba.fastjson.JSON;
import com.yang.sms.core.SendPostProcessor;
import com.yang.sms.core.SendResponse;
import com.yang.sms.entity.SmsRecord;
import com.yang.sms.service.SmsRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;

/**
 * @author Billyang
 * @date 2024/9/27 18:28
 */
@Component
public class MysqlSendPostProcessor implements SendPostProcessor {
    @Autowired
    private SmsRecordService smsRecordService;

    @Override
    public void afterSend(String channel, String mobile, String signName, String templateCode,
                          LinkedHashMap<String, String> templateParamMap, String content, SendResponse sendResponse) {

        SmsRecord smsRecord = new SmsRecord();
        smsRecord.setChannel(channel);
        smsRecord.setMobile(mobile);
        smsRecord.setSignName(signName);
        smsRecord.setTemplateCode(templateCode);
        smsRecord.setTemplateParamJson(JSON.toJSONString(templateParamMap));
        smsRecord.setContent(content);

        if (sendResponse.isSuccess()) {
            smsRecord.setResult("success");
        } else {
            smsRecord.setResult("fail");
            smsRecord.setMessage(sendResponse.getMessage());
        }
        smsRecord.setRequestId(sendResponse.getRequestId());
        smsRecordService.insert(smsRecord);
    }
}
